package com.shiwaixiangcun.authz.config;

import com.shiwaixiangcun.core.plugin.cache.CacheKeyGenerator;
import com.shiwaixiangcun.core.plugin.cache.SimpleCacheKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by SilentWu on 2017/3/14.
 */
@Configuration
@EnableCaching(proxyTargetClass = true)
public class CacheConfig extends CachingConfigurerSupport {

    @Autowired
    private Environment env;

    @Bean
    public JedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
        JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
        redisConnectionFactory.setHostName(env.getProperty("redis.host"));
        redisConnectionFactory.setPort(Integer.valueOf(env.getProperty("redis.port")));
        redisConnectionFactory.setPassword(env.getProperty("redis.password"));
        redisConnectionFactory.setTimeout(Integer.valueOf(env.getProperty("redis.timeout")));
        redisConnectionFactory.setPoolConfig(jedisPoolConfig);
        redisConnectionFactory.setDatabase(Integer.valueOf(env.getProperty("redis.db.index")));
        return redisConnectionFactory;
    }


    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(Integer.valueOf(env.getProperty("redis.maxIdle")));
        jedisPoolConfig.setMaxIdle(Integer.valueOf(env.getProperty("redis.maxActive")));
        jedisPoolConfig.setMaxWaitMillis(Integer.valueOf(env.getProperty("redis.maxWait")));
        jedisPoolConfig.setTestOnBorrow(Boolean.valueOf(env.getProperty("redis.testOnBorrow")));
        return jedisPoolConfig;
    }

    /**
     * 缓存管理器
     *
     * @param redisTemplate
     * @return
     */
    @Bean(name = "springCacheManager")
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        //设置过期时间为0，表示永不过期
        cacheManager.setDefaultExpiration(Long.valueOf(env.getProperty("redis.cache.default.expireTime")));
        cacheManager.setExpires(getCacheExpires());
        cacheManager.setUsePrefix(true);
        return cacheManager;
    }

    //配置缓存过期时间
    private Map<String, Long> getCacheExpires() {
        Map<String, Long> expires = new HashMap<>();
        //expires.put("user",3000l);
        expires.put("findBackPassword", 1200l);
        expires.put("VerifyCodeCacheName",  120L);
        return expires;
    }


    @Bean
    public CacheKeyGenerator cacheKeyGenerator() {
        return new SimpleCacheKeyGenerator("property");
    }

}
